package com.bw.service.impl;

import com.bw.domain.Role;
import com.bw.domain.TbMenu;
import com.bw.domain.User;
import com.bw.exception.MyRuntimeException;
import com.bw.mapper.UserMapper;
import com.bw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Wrj
 * @Date: 2025/3/8 08:31
 * @Description:
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User login(String username) {
        return userMapper.login(username);
    }

    @Override
    public List<Role> listRole(Integer userId) {
        return userMapper.listRole(userId);
    }

    @Override
    public List<TbMenu> listMenus(Integer userId) {
        return userMapper.listMenus(userId);
    }

    @Override
    public List<User> list(User user) {
        return userMapper.list(user);
    }

    @Override
    public List<Role> listRoleName() {
        return userMapper.listRoleName();
    }

    @Override
    public int checkedUsername(String username) {
        return userMapper.checkedUsername(username);
    }

    @Transactional(rollbackFor = MyRuntimeException.class)
    @Override
    public boolean add(User user) {

        try {

            userMapper.add(user);


            userMapper.instrUserRole(user.getUserId(),user.getRoleId());

            return true;
        }catch (MyRuntimeException e){
            e.printStackTrace();
            throw new MyRuntimeException("添加失败");

        }

    }

    @Override
    public List<User> listUser() {
        return userMapper.listUser();
    }

    @Override
    public void addBach(List<User> userList) {
        for (User user : userList) {
            userMapper.addBeach(user);
        }
    }

    @Override
    public List<User> listByIds(String ids) {
        return userMapper.listByIds(ids);
    }
}
